# app/__init__.py
import os

from flask_restx import Api, Resource
from flask_script import Manager

from flask import Flask
# 后端服务器配置 CORS (Cross-Origin Resource Sharing)
from flask_cors import CORS

# 导入 JWTManager：
from flask_jwt_extended import JWTManager
from datetime import timedelta

from .xxl_job_client import XXLJobClient

from flask_caching import Cache

# app是实例名
app = Flask(__name__)
# 后端服务器配置 CORS (Cross-Origin Resource Sharing)
CORS(app, resources={r"/*": {"origins": "*"}})  # 允许所有来源

# api = Api(app) 这句通常应该放在应用的初始化文件中（__init__.py）而不是放在应用的入口文件（app1.py 或 app2.py）中。
# 这是因为 Api(app) 创建了一个 Flask-RESTx 的应用实例，它需要与应用的上下文一起工作。
# 将它放在 __init__.py 中确保了 API 与应用的上下文正确绑定。
# api = Api(app)
api = Api(app, version='1.0',
          title='Zbfwb Cmdb API',
          description='A Cmdb API',
          # 在文档中全局登录，传token
          security="Bearer Auth",
          authorizations={
              "Bearer Auth": {
                  "type": "apiKey",
                  "in": "header",
                  "name": "Authorization",
                  "description": "Add a jwt with ** Bearer token"
              }
          }
          )

# app绑定 JWTManager 全局引入JWTManager
jwt = JWTManager(app)


# 初始化缓存
cache = Cache(app)
# 或者使用 init_app 方式
# cache = Cache()
# cache.init_app(app)

# #包名为python-dotenv
# from dotenv import load_dotenv  # 导入加载环境变量的函数
# import os
from flask_sqlalchemy import SQLAlchemy
from config import Config  # 导入配置

# # 使用 python-dotenv 加载环境变量
# load_dotenv()
#
# # 设置应用的配置选项
# app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
# print(os.getenv('SECRET_KEY'))
# app.config['DATABASE_URL'] = os.getenv('DATABASE_URL')

# 其他应用初始化和扩展...

# 加载配置
app.config.from_object(Config)
# 配置 JWT 访问令牌的有效期为 24 小时
# 动态扩展配置 config 的配置数据项
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(hours=24)

print(app.config['SQLALCHEMY_DATABASE_URI'])

# 创建数据库实例
db = SQLAlchemy(app)

# manager = Manager(app)

# 导入视图和模型
from app1 import routes, models
# 导入命名空间
from app1.routes import ns1
from app1.auth import auth_ns

# 将命名空间添加到 API 中
api.add_namespace(auth_ns)
# 添加命名空间
api.add_namespace(ns1)

# 导入alarm命名空间
from app1 import routes_alarm
from app1.routes_alarm import ns_alarm

api.add_namespace(ns_alarm)

# 导入kms_alarm命名空间
from app1 import routes_kms_alarm
from app1.routes_kms_alarm import ns_kms_alarm

api.add_namespace(ns_kms_alarm)

# 导入remedy_gongdan命名空间
from app1 import routes_remedy_gongdan
from app1.routes_remedy_gongdan import ns_remedy_gongdan

api.add_namespace(ns_remedy_gongdan)

# 导入machin_cmdb命名空间
from app1 import routes_machine_cmdb
from app1.routes_machine_cmdb import ns_machine_cmdb

api.add_namespace(ns_machine_cmdb)

# 引入 drip 命名空间
from app1 import routes_drip
from app1.routes_drip import ns_drip

api.add_namespace(ns_drip)

# 引入 websocket 命名空间
from app1 import wsserver
from app1.wsserver import ns_websocket

api.add_namespace(ns_websocket)

# 引入renedy_biangeng命名空间
from app1 import routes_remedy_biangeng
from app1.routes_remedy_biangeng import ns_remedy_biangeng

api.add_namespace(ns_remedy_biangeng)

# alarm rpt
from app1 import routes_alarm_rpt
from app1.routes_alarm_rpt import ns_alarm_rpt

api.add_namespace(ns_alarm_rpt)

from app1 import routes_sjfx
from app1.routes_sjfx import ns_sjfxpt

api.add_namespace(ns_sjfxpt)

# 此句必须要写，不然模块无法被引入
from app1 import ws_sjfx

# 导入命名空间
from app1 import auth_rbac
from app1.auth_rbac import auth_rbac_ns

api.add_namespace(auth_rbac_ns)

# 导入一体化
from app1 import routes_yth
from app1.routes_yth import ns_yth

api.add_namespace(ns_yth)

# 导入zos
from app1 import routes_zos
from app1.routes_zos import ns_zos

api.add_namespace(ns_zos)

# role
from app1 import routes_role
from app1.routes_role import ns_role

api.add_namespace(ns_role)

# perm
from app1 import routes_perm
from app1.routes_perm import ns_perm

api.add_namespace(ns_perm)

# user role
from app1 import routes_user_role
from app1.routes_user_role import ns_user_role

api.add_namespace(ns_user_role)

# role perm
from app1 import routes_role_perm
from app1.routes_role_perm import ns_role_perm

api.add_namespace(ns_role_perm)

from app1 import routes_xxljob
from app1.routes_xxljob import ns_xxl

api.add_namespace(ns_xxl)

from app1 import routes_wkflow
from app1.routes_wkflow import ns_cmn

api.add_namespace(ns_cmn)

from app1 import routes_patrol
from app1.routes_patrol import ns_patrol

api.add_namespace(ns_patrol)

# 注册巡检应用定义模块
from app1 import routes_patrol_app_def
from app1.routes_patrol_app_def import ns_app_def

api.add_namespace(ns_app_def, path='/api/app_def')


# 导入应用信息管理
from app1 import routes_app_info
from app1.routes_app_info import ns_app_info

api.add_namespace(ns_app_info)


# 导入 patrol_board 命名空间
from app1 import routes_patrol_board
from app1.routes_patrol_board import ns_patrol_board

api.add_namespace(ns_patrol_board, path='/api/patrol_board')